package cc.miwuya.property.service.impl;

import cc.miwuya.common.util.JwtUtils;
import cc.miwuya.common.util.Result;
import cc.miwuya.property.client.PropertyClient;
import cc.miwuya.property.client.ResidentialClient;
import cc.miwuya.property.client.UserClient;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cc.miwuya.property.domain.Parking;
import cc.miwuya.property.service.ParkingService;
import cc.miwuya.property.mapper.ParkingMapper;
import io.jsonwebtoken.Claims;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 杩烽浘妫灄
* @description 针对表【parking(车位表)】的数据库操作Service实现
* @createDate 2024-02-22 19:49:08
*/
@Service
public class ParkingServiceImpl extends ServiceImpl<ParkingMapper, Parking>
    implements ParkingService{
    @Resource
    UserClient userClient;
    @Resource
    PropertyClient propertyClient;
    @Resource
    ResidentialClient residentialClient;
    @Resource
    HttpServletRequest httpServletRequest;
    @Override
    public Object info(Long parkingId) {
        Parking parking = getById(parkingId);
        //获取业主信息
        Result info = userClient.ownerInfo(parking.getId());
        return MapUtil.builder()
                .put("parking",parking)
                .put("owner",info.getData())
                .build();
    }

    @Override
    public Object getPage(Page page, Long residentialId, String name) {
        Page<Parking> parkingPage = null;
        if(StrUtil.isEmpty(name)){
            parkingPage = page(page, new QueryWrapper<Parking>()
                    .eq("residential_id",residentialId));
        }else{
            parkingPage = page(page, new QueryWrapper<Parking>().eq("residential_id",residentialId).like("address",name));
        }
        return parkingPage;
    }

    @Override
    public Object getPage(Page page, String name) {
        Page<Parking> parkingPage = null;
        Long propertyId = propertyClient.getPropertyByUserid(getCurrent());
        List<Long> id = residentialClient.getResidentialIdByPropertyId(propertyId);
        if(StrUtil.isEmpty(name)||name.equals("null")){
            parkingPage = page(page, new QueryWrapper<Parking>()
                    .in("residential_id",id));
        }else{
            parkingPage = page(page, new QueryWrapper<Parking>().in("residential_id",id).like("address",name));
        }
        return parkingPage;
    }

    @Override
    public Object getOwnerParkingList(Page page, Long ownerId,String name) {
        Page<Parking> parkingPage = null;
        if(StrUtil.isEmpty(name)||name.equals("null")){
            parkingPage = page(page,new QueryWrapper<Parking>().eq("user_id",ownerId));
        }else {
            parkingPage = page(page,new QueryWrapper<Parking>().eq("user_id",ownerId).like("address",name));
        }
        return parkingPage;
    }

    /**
     * 获取当前登录人的身份
     */
    public Long getCurrent() {
        String authorization = httpServletRequest.getHeader("Authorization");
        Claims claim = JwtUtils.getClaimByToken(authorization, "community");
        JSON parse = JSONUtil.parse(claim);
        Long username = Long.parseLong(parse.getByPath("userId").toString());
        return username;
    }
}




